package com.ruoyi.web.core.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-01-12 20:28:49
 */
@Data
@Configuration
@ConditionalOnClass(WxMaService.class)
@ConfigurationProperties(prefix = "wechat.miniapp")
public class WxMaConfiguration {

    /**
     * 设置微信小程序的appId
     */
    private String appId;

    /**
     * 设置微信小程序的Secret
     */
    private String appSecret;

    @Bean
    @ConditionalOnMissingBean(WxMaService.class)
    public WxMaService wxMaService() {

        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(this.getAppId());
        config.setSecret(this.getAppSecret());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);

        return service;

    }
}
